<?php
class code extends Mod
{
	public $conn;
	
	public function __construct()
	{
		$this->conn = new Sqlite();
		$this->conn->connection($GLOBALS['dance_run']['db_name']);
	}
	
	
	public function view()
	{
		$text = isset($_GET['text']) ? Unit::safe($_GET['text']) : '';
		$qrcode = new QRcode();
		ob_clean();
		$qrcode::png($text, false, QR_ECLEVEL_L, 20, 2, false);
		// 参数:p1:二维码包含的内容 p2:输出的文件名 p3:容错级别 p4:大小 p5:外边距margin p6:是否保存并打印(false 直接生成 true 生成且打印)
	}
	
	public function save()
	{
		$res = [];
		$this->conn->sql = "select * from token where is_used=0 limit 0,200";
		$data = $this->conn->rows();
		foreach($data as $key => $item) {
			$id = $item['id'];
			$token = $item['token'];
			$qrdir = WWWROOT . 'qrcode' . DS;
			$qrcode = new QRcode();
			ob_clean();
			$qrcode::png($token, $qrdir . $id . '.png', QR_ECLEVEL_L, 20, 2, false);
			$sqlstr = "update token set is_used=1 where id=$id";
			$this->conn->sql = $sqlstr;
			$res[] = $this->conn->execute();
		}
		
		var_dump($res);
	}
	
	public function generate()
	{
		$id = 1;
		$time = time();
		$str = $id . '-' . $time;
		$code = substr(strtoupper(md5(md5($str))), 0, 10);
		echo $id . '-' . $time;;
		echo '<br />';
		echo $code;
	}
}